package com.houxy.service.impl;

import com.houxy.repository.mapper.ExpertMapper;
import com.houxy.repository.model.bo.ExpertBO;
import com.houxy.repository.model.entity.Expert;
import com.houxy.service.DepartmentService;
import com.houxy.service.ExpertService;
import com.houxy.service.HospitalService;
import com.houxy.service.UserService;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hou
 * @date 2019/3/18 15:53
 * @describe：
 */
@Service
public class ExpertServiceImpl implements ExpertService {

    @Autowired
    private ExpertMapper expertMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private HospitalService hospitalService;

    @Override
    public boolean addExpert(Expert expert) {
        int insert = expertMapper.insertSelective(expert);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<ExpertBO> findAll() {
        List<Expert> expertList = expertMapper.findAll();
        List<ExpertBO> expertBOList = new ArrayList<>();
        if (expertList != null) {
            expertList.stream().forEach(expert -> {
                ExpertBO expertBO = BeanMapper.map(expert, ExpertBO.class);
                expertBO.setDepartment(departmentService.findById(expert.getDepartmentId()));
                expertBO.setHospital(hospitalService.findById(expertBO.getHospitalId()));
                expertBOList.add(expertBO);
            });
            return expertBOList;
        }
        return null;
    }

    @Override
    public boolean deleteExper(int expertId) {
        ExpertBO expertBO = findAllById(expertId);
        if (expertBO != null) {
            Expert expert = BeanMapper.map(expertBO, Expert.class);
            expert.setIsDelete(1);
            int update = expertMapper.updateByPrimaryKey(expert);
            if (update > 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public List<ExpertBO> findByHospitalId(int hospitalId) {
        List<Expert> expertList = expertMapper.findByHospital(hospitalId);
        List<ExpertBO> expertBOList = new ArrayList<>();
        if (expertList != null) {
            expertList.stream().forEach(expert -> {
                ExpertBO expertBO = BeanMapper.map(expert, ExpertBO.class);
                expertBO.setDepartment(departmentService.findById(expert.getDepartmentId()));
                expertBO.setHospital(hospitalService.findById(expertBO.getHospitalId()));
                expertBOList.add(expertBO);
            });
            return expertBOList;
        }
        return null;
    }

    @Override
    public ExpertBO findAllById(int expertId) {
        Expert expert = expertMapper.selectByPrimaryKey(expertId);
        ExpertBO expertBO = new ExpertBO();
        if (expert != null) {
            expertBO = BeanMapper.map(expert, ExpertBO.class);
            expertBO.setDepartment(departmentService.findById(expertBO.getDepartmentId()));
            expertBO.setHospital(hospitalService.findById(expertBO.getHospitalId()));
        }
        return expertBO;
    }

    @Override
    public List<ExpertBO> findAllByCondition(Integer hospitalId, Integer departmentId, String type, String name) {
        List<Expert> expertList = expertMapper.findAllByCondition(hospitalId, departmentId, type, name);
        List<ExpertBO> expertBOList = new ArrayList<>();
        if (expertList != null) {
            expertList.stream().forEach(expert -> {
                ExpertBO expertBO = BeanMapper.map(expert, ExpertBO.class);
                expertBO.setDepartment(departmentService.findById(expert.getDepartmentId()));
                expertBO.setHospital(hospitalService.findById(expertBO.getHospitalId()));
                expertBOList.add(expertBO);
            });
            return expertBOList;
        }
        return null;
    }

    @Override
    public Expert findByUserName(String userName) {
        return expertMapper.findByUserName(userName);
    }


}
